<%--
  Created by IntelliJ IDEA.
  User: Albert Lee
  Date: 2020/5/19
  Time: 16:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>


<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML>
<html>
<head>
    <base href="<%=basePath%>">

    <title>聊天</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="refresh"content="10;url=know2.jsp">
</head>
<style>
    .container{
        position:relative;
        top:100px;
        margin:0 auto;
        width: 500px;
        height: 300px;
        border: 1px solid #aaa;
        overflow: hidden;
    }
    .usrlist{
        width: 100px;
        height: 250px;
        background-color: #bbb;
        display: block;
        float: left;
        overflow: scroll;
    }
    .chartinfo{
        width: 400px;
        height: 250px;
        background-color: #ccc;
        display: block;
        float: left;
        overflow: scroll;
    }
    .send{
        width: 500px;
        height: 50px;
        background-color: #ddd;
        display: block;
        float: left;
    }
    select{
        width: 50px;
    }
</style>
<%
    List<String> chartInfo = new  ArrayList<String>();//保存聊天信息的集合
    List<String> usrs1 =new  ArrayList<String>();//保存登录用户的集合
    if(null!=application.getAttribute("chartinfo")){
        chartInfo =  (List<String>)application.getAttribute("chartinfo");
    }
    if(null!=application.getAttribute("users")){
        usrs1  =  (List<String>) application.getAttribute("users");
    }
    //form 提交过来的数据
    String username = request.getParameter("username");
    String say = request.getParameter("gang");
    String to_usr = request.getParameter("tousr");

    if(null!=username&&!"".equals(username)){
        if(!usrs1.contains(username)){
            usrs1.add(username);
            session.setAttribute("users",username);
        }
    }
    application.setAttribute("users",usrs1);//将user1集合放入application

    String lgusr = "";
    if(null!=session.getAttribute("users")){
        lgusr = (String)session.getAttribute("users");
    }
    if(lgusr.equals("")||null==lgusr){
        response.setStatus(response.SC_MOVED_TEMPORARILY);
        response.setHeader("Location", "know.jsp");
    }
    String chart="";
    if(null!=to_usr&&null!=say&&!"".equals(to_usr)&&!"".equals(say)){
        chart = lgusr+" 对   "+to_usr+"说: "+say;
    }

    if(!"".equals(chart)){
        chartInfo.add(chart);
    }
    application.setAttribute("chartinfo", chartInfo);//将聊天信息集合 放入 application
%>

<body>

<div class="container">
    <div class="usrlist">
        <%
            List<String> usrs  =  (List<String>) application.getAttribute("users");
            if(usrs.size()>0){
                for(String u : usrs){
                    out.print(u);
                    out.print("\n");
                }
            }
        %>
    </div>
    <div class="chartinfo">
        <%
            //   out.print(chartInfo.size());
            for(String ct : chartInfo){
                out.print(ct);
                out.print("\n");
            }
        %>
    </div>
    <div class="send">
            <input type="text" name="gang"style="width: 300px;">
            <input type="submit" value="发送">
    </div>
</div>
</body>
</html>
